package top.cardone.context.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by cardone-home-001 on 2016/4/20.
 */
public class ListUtils extends org.apache.commons.collections.ListUtils {
	/**
	 * 转换集合
	 *
	 * @param list        原集合
	 * @param configTable 配置
	 * @param <V>         类型
	 * @return 新集合
	 */
	public static <V> List<Map<String, V>> newArrayList(List<Map<String, V>> list, Table<String, String, String> configTable) {
		List<Map<String, V>> newList = Lists.newArrayList();

		if (CollectionUtils.isEmpty(list)) {
			return newList;
		}

		for (Map<String, V> map : list) {
			newList.add(MapUtils.newHashMap(map, configTable));
		}

		return newList;
	}

	/**
	 * 转换集合
	 *
	 * @param <V>       类型
	 * @param list      原集合
	 * @param mapperMap 配置
	 * @return 新集合
	 */
	public static <V> List<Map<String, V>> newArrayList(List<Map<String, V>> list, Map<String, Object> mapperMap) {
		List<Map<String, V>> newList = Lists.newArrayList();

		if (CollectionUtils.isEmpty(list)) {
			return newList;
		}

		for (Map<String, V> map : list) {
			newList.add(MapUtils.newHashMap(map, mapperMap));
		}

		return newList;
	}

	public static <V> List<Map<String, V>> newArrayList(List<Map<String, V>> list, String resourcePath, boolean cache) {
		return newArrayList(list, TableUtils.newTable(resourcePath, cache));
	}
}